package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.method.ComponentInterface;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IComponentInterfaceDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("componentInterfaceDao")
public class ComponentInterfaceDaoImpl implements IComponentInterfaceDao {

	@Resource(name = "baseDao")
	private IBaseDao<ComponentInterface> baseDao;

	private static final String URI = "uri";
	
	private static final String DELETED = "deleted";

	private static final String URI_INTERFACE = "interface";

	@Override
	public void save(ComponentInterface componentInterface) {
		String temp = componentInterface.getName() + System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!checkUri(URI_INTERFACE + ":" +uri)) {
			temp = componentInterface.getName() + System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		componentInterface.setUri(URI_INTERFACE + ":" + uri);
		baseDao.save(componentInterface);
	}

	@Override
	public void update(ComponentInterface componentInterface) {
		baseDao.update(componentInterface);
	}

	@Override
	public ComponentInterface queryById(long id) {
		return baseDao.queryById(ComponentInterface.class, id);
	}

	@Override
	public boolean checkUri(String uri) {
		// TODO Auto-generated method stub
		ComponentInterface cinInterface = queryByUri(uri);
		if (cinInterface != null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public ComponentInterface queryByUri(String uri) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from ComponentInterface where ");
		hql.append(URI + "=");
		hql.append("'" + uri + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public List<ComponentInterface> queryAll() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from ComponentInterface where ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public PageBean<ComponentInterface> queryByPageNum(int pageNo)
			throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from ComponentInterface where ");
		hql.append(DELETED + "=0");
		PageBean<ComponentInterface> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public boolean delete(ComponentInterface componentInterface) {
		// TODO Auto-generated method stub
		componentInterface.setDeleted(true);
		baseDao.update(componentInterface);
		return true;
	}

	@Override
	public boolean deleteById(long id) {
		// TODO Auto-generated method stub
		ComponentInterface componentInterface = queryById(id);
		componentInterface.setDeleted(true);
		baseDao.update(componentInterface);
		return true;
	}

	@Override
	public boolean deleteByUri(String uri) {
		// TODO Auto-generated method stub
		ComponentInterface componentInterface = queryByUri(uri);
		componentInterface.setDeleted(true);
		baseDao.update(componentInterface);
		return true;
	}

	@Override
	public List<ComponentInterface> queryByProperty(String text) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from ComponentInterface a where ");
		hql.append(" a.name like '%");
		hql.append( text+ "%' or ");
		hql.append(" a.description like '%");
		hql.append( text+ "%' ");
		return baseDao.query(hql.toString());
	}

	@Override
	public List<ComponentInterface> queryByType(String type) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from ComponentInterface a where ");
		hql.append(" a.itype = '");
		hql.append( type+ "' and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

}
